package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.config.ConfigFile;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import com.fwtai.tool.ToolString;
import com.fwtai.web.InformDao;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 通知|公告业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021-08-24 16:23:58
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class InformService{

    @Resource
    private InformDao informDao;

    @Resource
    private ThreadPoolTaskExecutor taskExecutor;

    public String add(final HttpServletRequest request){
        final PageFormData formData = new PageFormData(request);
        final String p_title = "title";
        final String p_flag = "flag";
        final String p_details = "details";
        final String validate = ToolClient.validateField(formData,p_title,p_flag,p_details);
        if(validate != null)return validate;
        if(formData.getString(p_title).length() > 64){
            return ToolClient.createJsonFail("标题内容字数太长");
        }
        final String fieldInteger = ToolClient.validateInteger(formData,p_flag);
        if(fieldInteger != null)return fieldInteger;
        if(formData.getString(p_details).length() > 512){
            return ToolClient.createJsonFail("内容信息内容字数太长");
        }
        final String url = formData.getString("url");
        if(url != null){
            if(!url.startsWith("http")){
                return ToolClient.createJsonFail("跳转url必须以http开头");
            }
        }
        formData.put("kid",new IdBuilder().getId121L());
        return ToolClient.executeRows(informDao.add(formData));
    }

    public String edit(final HttpServletRequest request){
        final PageFormData formData = new PageFormData(request);
        final String p_kid = "kid";
        final String p_title = "title";
        final String p_flag = "flag";
        final String p_details = "details";
        final String validate = ToolClient.validateField(formData,p_title,p_flag,p_details,p_kid);
        if(validate != null)return validate;
        if(formData.getString(p_title).length() > 64){
            return ToolClient.createJsonFail("标题内容字数太长");
        }
        final String fieldInteger = ToolClient.validateInteger(formData,p_flag);
        if(fieldInteger != null)return fieldInteger;
        if(formData.getString(p_details).length() > 512){
            return ToolClient.createJsonFail("内容信息内容字数太长");
        }
        final String url = formData.getString("url");
        if(url != null){
            if(!url.startsWith("http")){
                return ToolClient.createJsonFail("跳转url必须以http开头");
            }
        }
        final String exist_key = informDao.queryExistById(formData.getString(p_kid));
        if(exist_key == null){
            return ToolClient.createJsonFail("数据不存在,刷新重试");
        }
        return ToolClient.executeRows(informDao.edit(formData));
    }

    public String queryById(final PageFormData formData){
        final String p_id = "id";
        final String validate = ToolClient.validateField(formData,p_id);
        if(validate != null)return validate;
        return ToolClient.queryJson(informDao.queryById(formData.getString(p_id)));
    }

    public String delById(final PageFormData formData){
        final String p_kid = "id";
        final String validate = ToolClient.validateField(formData,p_kid);
        if(validate != null)return validate;
        final String kid = formData.getString(p_kid);
        final String exist_key = informDao.queryExistById(kid);
        if(exist_key == null){
            return ToolClient.createJsonFail("删除失败,数据已不存在");
        }
        return ToolClient.executeRows(informDao.delById(kid));
    }

    public String delByKeys(final PageFormData formData){
        final String p_ids = "ids";
        final String validate = ToolClient.validateField(formData,p_ids);
        if(validate != null)return validate;
        final String ids = formData.getString(p_ids);
        final ArrayList<String> lists = ToolString.keysToList(ids);
        if(lists == null || lists.size() <= 0){
            return ToolClient.createJsonFail("请选择要删除的数据");
        }
        return ToolClient.executeRows(informDao.delByKeys(lists),"操作成功","数据不存在,刷新重试");
    }

    public String listData(PageFormData formData){
        try {
            formData = ToolClient.dataTableMysql(formData);
            if(formData == null)return ToolClient.jsonValidateField();
            final HashMap<String,Object> map = informDao.listData(formData);
            return ToolClient.dataTableOK((List<Object>)map.get(ConfigFile.rows),map.get(ConfigFile.total));
        } catch (Exception e){
            return ToolClient.dataTableException(formData.get("sEcho"));
        }
    }

    //线程池异步查询
    public String getListData(PageFormData formData){
      try {
        formData = ToolClient.dataTableMysql(formData);
        if(formData == null)return ToolClient.jsonValidateField();
        final PageFormData params = formData;
        final CompletableFuture<List<Map<String,Object>>> asyncListData = CompletableFuture.supplyAsync(() -> informDao.getListData(params),taskExecutor);
        final CompletableFuture<Integer> asyncListTotal = CompletableFuture.supplyAsync(() -> informDao.getListTotal(params),taskExecutor);
        return ToolClient.dataTableOK(asyncListData.join(),asyncListTotal.join());
      } catch (Exception e){
        return ToolClient.dataTableException(formData.get("sEcho"));
      }
    }
}